/**
 * Urban Parks
 *
 * Team M&M's:
 * Michael Satran Jr
 * Michael Pogson
 * Stephen Hunter
 *
 * Date: Feb 16, 2012
 */
package view;

import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import model.Job;
import model.Park;

import controller.Controller;

/**
 * @author Michael Satran Jr
 */
public class VolunteerPanel extends UserPanel {

	/**
	 * Reference to the controller.
	 */
	private Controller my_controller;
	
	/**
	 * Main frame to display the VolunteerPanel.
	 */
	private JFrame my_frame;
	
	/**
	 * Main panel to contain the VolunteerPanel
	 */
	private JPanel my_panel;
	
	/**
	 * A table which holds the jobs the volunteer is currently signed up for.
	 */
	private JTable my_jobs;
	
	/**
	 * Contains all parks in the system.
	 */
	private JComboBox my_parks;
	/**
	 * Contains all the districts in the system.
	 */
	private JComboBox my_districts;
	/**
	 * Contains all the regions in the system.
	 */
	private JComboBox my_regions;
	
	/**
	 * Contains the searched jobs which are the open jobs for a volunteer to sign up for.
	 */
	private JTable my_searched_jobs;
	/**
	 * Button which causes searched jobs to display based on the selected park, district, and region.
	 */
	private JButton my_search_button;
	
	/**
	 * Constructs a new VolunteerPanel
	 * @param the_controller A reference to the controller.
	 */
	public VolunteerPanel(Controller the_controller) {
		my_controller = the_controller;
		my_frame = new JFrame("Urban Parks - Volunteer");
		
		/*//Listener for when the window is closed.
		my_frame.addWindowListener(new WindowAdapter() {
	    	public void windowClosing(WindowEvent e) {
	    		List<String[]> job_lines = Main.reParseJobs((List<Job>) my_controller.getAllJobs());
	    		Main.writeToFile(new File("jobs.txt"), job_lines);
	    		
	    		List<String[]> user_lines = Main.reParseUsers(my_controller.getUsers());
	    		Main.writeToFile(new File("users.txt"), user_lines);
	    	}
	    	public void windowClosed(WindowEvent e) {
	    		windowClosing(e);
	    		GUI mainGUI = new GUI(my_controller);
				mainGUI.start();
	    	}
	    });*/

		my_panel = new JPanel();
		my_jobs = new JTable();
		
		my_parks = new JComboBox();
		my_districts = new JComboBox();
		my_regions = new JComboBox();
		my_search_button = new JButton("Refine Open Jobs");
		my_searched_jobs = new JTable();
	}

	
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void start() {
		super.start(my_frame, my_panel, my_controller);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setup() {
		
		setupTable(my_jobs, (List<Job>)my_controller.getJobsForVolunteer(), new String[]{"Jobs currently signed up for"});
		setupTable(my_searched_jobs, (List<Job>)my_controller.getOpenJobs(), new String[]{"Open jobs currently available"});
		
		List<String> park_names = (List<String>)my_controller.getParkNames();
		List<String> district_names = (List<String>)my_controller.getDistricts();
		List<String> region_names = (List<String>)my_controller.getRegions();
		
		park_names.add(0, "All");
		district_names.add(0, "All");
		region_names.add(0, "All");
		
		my_parks.setModel(new DefaultComboBoxModel(park_names.toArray()));
		my_districts.setModel(new DefaultComboBoxModel(district_names.toArray()));
		my_regions.setModel(new DefaultComboBoxModel(region_names.toArray()));
		
		my_search_button.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent the_arg) {
				try {
					String park = (String) my_parks.getSelectedItem();
					String district = (String) my_districts.getSelectedItem();
					String region = (String) my_regions.getSelectedItem();
					
					List<Job> open_jobs = (List<Job>) my_controller.getOpenJobs();
					
					if (!park.equals("All")) {
						open_jobs = (List<Job>) my_controller.getOpenJobsByPark(park, open_jobs);
					}
					
					if (!district.equals("All")) {
						open_jobs = (List<Job>) my_controller.getOpenJobsByDistrict(district, open_jobs);
					}
					
					if (!region.equals("All")) {
						open_jobs = (List<Job>) my_controller.getOpenJobsByRegion(region, open_jobs);
					}
					
					setupTable(my_searched_jobs, open_jobs, new String[]{"Open jobs currently available"});
				} catch(Exception e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE, null);
				}
			}
			
		});
		
		my_searched_jobs.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				try {
					int row = my_searched_jobs.getSelectedRow();
					Job job = (Job) my_searched_jobs.getValueAt(row, 0);
					int option = JOptionPane.showConfirmDialog(null,
							"Would you like to Sign up for this Job?", "Would you like to Sign up for this Job?", JOptionPane.YES_NO_OPTION);
					if (option == JOptionPane.YES_OPTION) {
						my_controller.addVolunteerToJob(job.getParkName(), job);
						setupTable(my_jobs, (List<Job>)my_controller.getJobsForVolunteer(), new String[]{"Jobs currently signed up for"});
					}
				} catch (Exception the_exception) {
					the_exception.printStackTrace();
					JOptionPane.showMessageDialog(null, the_exception.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE, null);
				}
			}
		});
		
		LayoutManager mainLayout = new BorderLayout();
		my_panel.setLayout(mainLayout);
		
		JPanel centerRegion = new JPanel();
		centerRegion.add(new JScrollPane(my_jobs));
		centerRegion.add(new JScrollPane(my_searched_jobs));
		
		JButton logout_button = new JButton("Logout");
		logout_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				my_frame.dispose();
			}
			
		});
		
		JPanel southRegion = new JPanel();
		southRegion.add(logout_button);
		southRegion.add(my_parks);
		southRegion.add(my_districts);
		southRegion.add(my_regions);
		southRegion.add(my_search_button);
		
		my_panel.add(southRegion, BorderLayout.SOUTH);
		my_panel.add(centerRegion, BorderLayout.CENTER);
		
	}

}
